/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Minh Luc
 */
public class SendMail {

    public static void send(String to, String subject, String body) throws Exception {
        Properties properties = System.getProperties();
        String smtpServer = "smtp.gmail.com";
        String from = " WebBasedClaimProcessing@gmail.com";
        String psw = "12345@abc";
        properties.put("mail.smtp.host", smtpServer);
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.starttls.enable", "true");
        final String login = from;//"nth001@gmail.com";//usermail
        final String pwd = psw;//"password cua ban o day";
        Authenticator pa = null; //default: no authentication
        if (login != null && pwd != null) { //authentication required?
            properties.put("mail.smtp.auth", "true");
            pa = new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(login, pwd);
                }
            };
        }
//else: no authentication
        Session session = Session.getInstance(properties, pa);
// — Create a new message –
        Message msg = new MimeMessage(session);
// — Set the FROM and TO fields –
        msg.setFrom(new InternetAddress(from));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));

// — Set the subject and body text –
        msg.setSubject(subject);
        msg.setText(body);
// — Set some other header information –
        msg.setHeader("X-Mailer", "LOTONtechEmail");
        msg.setSentDate(new Date());
        msg.saveChanges();
// — Send the message –
        Transport.send(msg);
        System.out.println("Message sent OK.");
    }
}
